package user

import (
	"api/common"
	"api/pb/user"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
	return &GetUserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// GetUserInfo 获取用户信息
func (l *GetUserInfoLogic) GetUserInfo() (resp *types.UserInfo, err error) {
	userID, err := common.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}

	u, err := l.svcCtx.UserRPC.GetUserByID(l.ctx, &user.GetUserByIDRequest{Id: userID})
	if err != nil {
		return nil, err
	}

	resp = &types.UserInfo{
		Id:        u.Id,
		Name:      u.Name,
		Mobile:    u.Mobile,
		Email:     u.Email,
		Avatar:    u.Avatar,
		Status:    u.Status,
		Create_at: u.CreateAt,
		Update_at: u.UpdateAt,
	}
	return resp, nil
}
